//
//  ChooseAddressTableViewController.m
//  1243
//
//  Created by 我的 on 16/5/4.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "gerAddressController.h"
#import "AddressTableViewCell.h"
#import "NetWorkTools.h"
#import "UIBarButtonItem+Item.h"
#import "CreatAddressViewController.h"
#import "SVProgressHUD.h"
#import "ChooseAddressTableViewController.h"
#import "ReadLoginData.h"
#import "ManagerViewController.h"
#import "UpDataViewController.h"
@interface ChooseAddressTableViewController ()<UIAlertViewDelegate>
{
    BOOL jilu;
}
@property (nonatomic,strong) NSArray *addressArray;

@property (nonatomic,assign)BOOL *isSvpShow;

@end

@implementation ChooseAddressTableViewController

- (NSArray *)addressArray {
    if (!_addressArray) {
        _addressArray = [NSArray array];
    }
    return _addressArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"收货地址";
    
    self.tableView.rowHeight = 100;
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithImage:nil highImage:nil target:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside title:@"修改"];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn];
    btn.frame = CGRectMake(40,HEIGHT - 50- 64 , WIDTH - 80, 50);
    [btn setBackgroundColor:NAVBAR_COLOR];
    [btn setTitle:@"新增地址" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(didCreatBarButton) forControlEvents:UIControlEventTouchUpInside];    [self loadUserAddressData];
}
- (void)btnAction{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请您选择要修改的地址" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
    jilu = YES;
    
}
- (void)didCreatBarButton {
    ManagerViewController *manager = [[ManagerViewController alloc] init];
    
    [self pushForm:self.navigationController pushToAndHidden:manager];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if (!self.isSvpShow && self.addressArray.count) {
        [SVProgressHUD show];
    }
    
    
    [self loadUserAddressData];
}


- (void)pushForm:(UINavigationController *)pushForm pushToAndHidden:(UIViewController *)pushToVC
{
    [pushToVC setHidesBottomBarWhenPushed:YES];
    [pushForm pushViewController:pushToVC animated:YES];
    
}


#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.addressArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([SVProgressHUD isVisible]) {
        [SVProgressHUD dismiss];
    }
    
    static NSString *identifier = @"OrderDetailTableViewCellget";
    AddressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"AddressTableViewCell" owner:nil options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    self.isSvpShow = YES;
    NSString *NameString = self.addressArray[indexPath.row][@"ContactName"];
    cell.NameLabel.text = [NSString stringWithFormat:@"姓名: %@",NameString];
    
    NSString *PhoneNum = self.addressArray[indexPath.row][@"ContactPhone"];
    cell.PhoneNameLabel.text = [NSString stringWithFormat:@"电话: %@",PhoneNum];
    
    NSString *AddressString = [NSString stringWithFormat:@"%@ %@ %@ %@ ",  self.addressArray[indexPath.row][@"Province"], self.addressArray[indexPath.row][@"City"],self.addressArray[indexPath.row][@"Countory"],self.addressArray[indexPath.row][@"Address"]];
    cell.AddressLabel.text = [NSString stringWithFormat:@"详细地址: %@",AddressString];
    
    
    return cell;
}


#pragma mark - 获取数据

- (void)loadUserAddressData {
    
    NSDictionary *userData = [ReadLoginData readLoginData];
    
    NSString *userId = [userData objectForKey:@"Number"];
    
    NSDictionary *params = @{
                             //                             @"UserId":@"1"
                             @"UserId":userId
                             };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"EXPRESSQUERY" completeBlock:^(id object) {
        //        NSLog(@"%@",object);
        
        NSLog(@"%@",[object objectForKey:@"BackData"]);
        
        NSString *string = [object objectForKey:@"BackData"];
        
        self.addressArray = [NetWorkTools arrayWithJsonString:string];
        
        [self.tableView reloadData];
        
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%ld",indexPath.row);
    
 
    
    if (jilu) {
        UpDataViewController *upData = [[UpDataViewController alloc] init];
        
        upData.addressDict = self.addressArray[indexPath.row];
        jilu = NO;
        [self pushForm:self.navigationController pushToAndHidden:upData];
        
    }else{
        
        NSDictionary *dict = [ReadLoginData readAddressData];
        
        if (dict.count) {
            
            [ReadLoginData removeDataToLocal:@"addressData.plist"];
            
            NSString *shengFen = [self.addressArray[indexPath.row] objectForKey:@"Province"];
            
            NSString *city = [self.addressArray[indexPath.row] objectForKey:@"City"];
            
            NSString *qu = [self.addressArray[indexPath.row] objectForKey:@"ContactName"];
            
            NSDictionary *dic = @{
                                  @"shengfen":shengFen,
                                  @"chengshi":city,
                                  @"name":qu,
                                  @"phone":[self.addressArray[indexPath.row] objectForKey:@"ContactPhone"],
                                  @"address":[self.addressArray[indexPath.row] objectForKey:@"Address"],
                                  @"youbian":[self.addressArray[indexPath.row] objectForKey:@"ContactPostCode"],
                                  @"shenShiQu":[NSString stringWithFormat:@"%@%@%@", shengFen, city, qu]
                                  };
            [ReadLoginData saveAddressDataToLocal:dic];
            
            [self.navigationController popViewControllerAnimated:YES];
            
        } else {
            NSString *shengFen = [self.addressArray[indexPath.row] objectForKey:@"Province"];
            
            NSString *city = [self.addressArray[indexPath.row] objectForKey:@"City"];
            
            NSString *qu = [self.addressArray[indexPath.row] objectForKey:@"ContactName"];
            
            NSDictionary *dic = @{
                                  @"shengfen":shengFen,
                                  @"chengshi":city,
                                  @"name":qu,
                                  @"phone":[self.addressArray[indexPath.row] objectForKey:@"ContactPhone"],
                                  @"address":[self.addressArray[indexPath.row] objectForKey:@"Address"],
                                  @"youbian":[self.addressArray[indexPath.row] objectForKey:@"ContactPostCode"],
                                  @"shenShiQu":[NSString stringWithFormat:@"%@%@%@", shengFen, city, qu]
                                  };
            [ReadLoginData saveAddressDataToLocal:dic];
            
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    }
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
//    NSLog(@"%ld",buttonIndex);
    
    if (buttonIndex == 0) {
        
        NSDictionary *dict = [ReadLoginData readAddressData];
        
        if (dict.count) {
            
            [ReadLoginData removeDataToLocal:@"addressData.plist"];
            
            NSString *shengFen = [self.addressArray[alertView.tag] objectForKey:@"Province"];
            
            NSString *city = [self.addressArray[alertView.tag] objectForKey:@"City"];
            
            NSString *qu = [self.addressArray[alertView.tag] objectForKey:@"ContactName"];
            
            NSDictionary *dic = @{
                                  @"shengfen":shengFen,
                                  @"chengshi":city,
                                  @"name":qu,
                                  @"phone":[self.addressArray[alertView.tag] objectForKey:@"ContactPhone"],
                                  @"address":[self.addressArray[alertView.tag] objectForKey:@"Address"],
                                  @"youbian":[self.addressArray[alertView.tag] objectForKey:@"ContactPostCode"],
                                  @"shenShiQu":[NSString stringWithFormat:@"%@%@%@", shengFen, city, qu]
                                  };
            [ReadLoginData saveAddressDataToLocal:dic];
            
            [self.navigationController popViewControllerAnimated:YES];
            
        } else {
            NSString *shengFen = [self.addressArray[alertView.tag] objectForKey:@"Province"];
            
            NSString *city = [self.addressArray[alertView.tag] objectForKey:@"City"];
            
            NSString *qu = [self.addressArray[alertView.tag] objectForKey:@"ContactName"];
            
            NSDictionary *dic = @{
                                  @"shengfen":shengFen,
                                  @"chengshi":city,
                                  @"name":qu,
                                  @"phone":[self.addressArray[alertView.tag] objectForKey:@"ContactPhone"],
                                  @"address":[self.addressArray[alertView.tag] objectForKey:@"Address"],
                                  @"youbian":[self.addressArray[alertView.tag] objectForKey:@"ContactPostCode"],
                                  @"shenShiQu":[NSString stringWithFormat:@"%@%@%@", shengFen, city, qu]
                                  };
            [ReadLoginData saveAddressDataToLocal:dic];
            
            [self.navigationController popViewControllerAnimated:YES];
        }
        
//        NSLog(@"确定");
    } else {
//        NSLog(@"取消");
    }
    
}

@end
